set.seed(15)
weight <- rnorm(100, mean = 60, sd = 15)
height <- rnorm(100, mean = 170, sd = 20)

# 合并方差计算函数
variance <- function(x) sum((x - mean(x))^2) / (length(x) - 1)
c(var(weight), variance(weight))
c(var(height), variance(height))

# BMI计算函数
BMI <- function(h, w) w / h^2
BMI(1.78, 70)

# 过滤均值函数
filtered_mean <- function(x, threshold = 60) mean(x[x > threshold])

# 绘图函数
plot_distribution <- function(data, breaks_seq, title, xlab, color = "skyblue") {
  bins <- cut(data, breaks = breaks_seq, right = FALSE)
  table_data <- table(bins)
  bp <- barplot(table_data, 
                main = title,
                xlab = xlab,
                ylab = "频数",
                col = color,
                border = "white",
                ylim = c(0, max(table_data) + 5))
  text(bp, table_data, labels = table_data, pos = 3, cex = 0.8, col = "black")
}

# 体重分布图
plot_distribution(weight, seq(15, 105, by = 10), "体重分布图", "体重/kg")

# 身高分布图
plot_distribution(height, seq(110, 230, by = 10), "身高分布图", "身高/cm")



